package com.example.demo.web.rest.controller;

import com.ffcs.oss.annotation.PermissionAuth;
import com.example.demo.client.evt.sysbasequery.*;
import com.example.demo.client.vm.sysbasequery.*;
import com.example.demo.domain.ResourceEntity;
import com.example.demo.format.TreeBuilder;
import com.ffcs.oss.param.out.ServiceResp;
import com.example.demo.security.AuthoritiesConstants;
import com.example.demo.security.SecurityUtils;
import com.example.demo.service.UserGroupService;
import com.example.demo.web.rest.evt.IdEvt;
import com.example.demo.web.rest.evt.RelationEvt;
import com.example.demo.web.rest.evt.SaveUserGroupEvt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户组管理
 * @Author zhengmf
 * @Date 2018/12/25  11:33
 */
@Api("用户组管理")
@RestController
@RequestMapping("/api/usergroup")
public class UserGroupController extends BaseController{
    @Autowired
    private UserGroupService userGroupService;

    @ApiOperation(value = "保存用户组关联用户")
    @PermissionAuth(code = "OSS-SETTING-USERGROUP-EDIT,OSS-SETTING-USERGROUP-ADD")
    @PostMapping("/saveUserGroupRelateUser")
    public ServiceResp<Integer> saveUserGroupRelateUser(@RequestBody @Validated RelationEvt evt){
        return ServiceResp.getInstance(Integer.class).success(userGroupService.addGroupUserByGroupIdAndUserIds(evt));
    }

    @ApiOperation(value = "移除用户组关联用户")
    @PermissionAuth(code = "OSS-SETTING-USERGROUP-EDIT,OSS-SETTING-USERGROUP-ADD")
    @PostMapping("/removeUserGroupRelateUser")
    public ServiceResp<Integer> removeUserGroupRelateUser(@RequestBody @Validated RelationEvt evt){
        return ServiceResp.getInstance(Integer.class).success(userGroupService.delGroupUserByGroupIdAndUserIds(evt));
    }

    @ApiOperation(value = "是否可以删除用户组")
    @PostMapping("/isCanDelUserGroup")
    public ServiceResp<Boolean> isCanDelUserGroup(@RequestBody @Validated IdEvt evt){
        evt.setLoginName(SecurityUtils.getCurrentUserLogin().get());
        return baseQueryClient.isCanDelUserGroup(evt);
    }

    @ApiOperation(value = "删除用户组")
    @PermissionAuth(code = "OSS-SETTING-USERGROUP-DEL")
    @PostMapping("/delUserGroup")
    public ServiceResp<Boolean> delUserGroup(@RequestBody @Validated IdEvt evt){
        evt.setLoginName(SecurityUtils.getCurrentUserLogin().get());
        ServiceResp resp = baseQueryClient.isCanDelUserGroup(evt);
        if(!(Boolean) resp.getBody()){
            return ServiceResp.getInstance().error(resp.getHead().getRespMsg());
        }
        userGroupService.delUserGroup(evt);
        return ServiceResp.getInstance(Boolean.class).success(true,"删除成功！");
    }

    @ApiOperation(value = "保存用户组")
    //@PermissionAuth(code = "OSS-SETTING-USERGROUP-EDIT,OSS-SETTING-USERGROUP-ADD")
    @PostMapping("/saveUserGroup")
    public ServiceResp saveUserGroup(@RequestBody @Validated SaveUserGroupEvt evt){
        IsGroupNameEqualEvt equalEvt = new IsGroupNameEqualEvt();
        equalEvt.setType(evt.getType());
        equalEvt.setGroupName(evt.getUserGroup().getGroupName());
        if(evt.getType() == 1){//新增
            equalEvt.setGroupId(evt.getUserGroup().getParentId());
        }else {
            equalEvt.setGroupId(evt.getUserGroup().getGroupId());
        }
        if(baseQueryClient.isGroupNameEqual(equalEvt).getBody()){
            //0-允许保存，1-数据库里有记录，不允许保存
            return ServiceResp.getInstance().error("用户组名称已存在！");
        }
        try{
            Long groupId = userGroupService.saveUserGroup(evt);
            baseQueryClient.clearQryCache();
            return ServiceResp.getInstance(Long.class).success(groupId,"保存成功");
        }catch (Exception e){
            return ServiceResp.getInstance().error("保存异常,"+e.getMessage());
        }
    }

    @ApiOperation("获取用户组树")
    @PostMapping("/getGroupTree")
    public ServiceResp<List<UserGroupTreeVm>> getGroupTreeOfGroup(@RequestBody UserGroupTreeEvt evt){
        evt.setAdmin(SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN));
        evt.setLoginName(SecurityUtils.getCurrentUserLogin().get());
        return baseQueryClient.getGroupTreeOfGroup(evt);
    }

    @ApiOperation("用户组管理-首页")
    @PostMapping("/getGroupTreeInfos")
    public ServiceResp<List<UserGroupTreeVm>> getGroupTreeInfos(@RequestBody UserGroupTreeEvt evt){
        evt.setAdmin(SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN));
        evt.setLoginName(SecurityUtils.getCurrentUserLogin().get());
        return baseQueryClient.getGroupTreeInfos(evt);
    }

    @ApiOperation("上级用户组下拉框值")
    @PostMapping(value = "/getParentGroupTree")
    public ServiceResp getParentGroupTree(@RequestBody IdEvt evt){
        return ServiceResp.getInstance(List.class).success(TreeBuilder.newTreeBuilder(UserGroupTreeVm.class, Long.class)
                .buildToTreeList(userGroupService.getParentGroupTree(evt)));
    }

    @ApiOperation("获取用户组下拉框数据")
    @PostMapping("/getUserGroupStaticData")
    public ServiceResp<UserGroupStaticDataVm> getUserGroupStaticData(){
        return baseQueryClient.getUserGroupStaticData();
    }

    @ApiOperation("根据ID获取用户组信息")
    @PostMapping("/getGroupById")
    public ServiceResp<UserGroupVm> getGroupById(@RequestBody @Validated IdEvt evt){
        evt.setLoginName(SecurityUtils.getCurrentUserLogin().get());
        return baseQueryClient.getGroupById(evt);
    }

    @ApiOperation("获取用户组管理角色树")
    @PostMapping("/getRoleTree")
    public ServiceResp<List<RoleTreeVm>> getRoleTreeOfGroup(@RequestBody @Validated GetTreeOfGroupEvt evt){
        evt.setLoginName(SecurityUtils.getCurrentUserLogin().get());
        return baseQueryClient.getRoleTreeOfGroup(evt);
    }

    @ApiOperation("获取用户组管理功能权限树")
    @PostMapping("/getPermissionTree")
    public ServiceResp<List<PermissionTreeVm>> getPermissionTreeOfUserGroup(@RequestBody @Validated GetTreeOfGroupEvt evt){
        evt.setLoginName(SecurityUtils.getCurrentUserLogin().get());
        return baseQueryClient.getPermissionTreeOfUserGroup(evt);
    }

    @ApiOperation("用户组管理资源权限树")
    @PostMapping("/getResourceTree")
    public ServiceResp<List<ResourceEntity>> getResourceTreeOfGroup(@RequestBody ResourceTreeOfGroupEvt evt){
        evt.setLoginName(SecurityUtils.getCurrentUserLogin().get());
        return baseQueryClient.getResourceTreeOfGroup(evt);
    }

    @ApiOperation("获得区域树：当前用户为超级管理员显示全部数据；当前用户为普通用户：显示当前用户所属区域及其子节点。")
    @PostMapping("/getRegionTree")
    public ServiceResp<List<QueryRegionListVm>> getRegionTree(@RequestBody LoginNameEvt evt){
        evt.setLoginName(SecurityUtils.getCurrentUserLogin().get());
        return baseQueryClient.getRegionTree(evt);
    }
}
